package controller;

import Utils.RespModel;
import entity.BannerEntity;
import org.apache.ibatis.jdbc.Null;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import service.serviceImpl.BannerService;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2018/4/9.
 */
@Controller
@RequestMapping("/banner")
public class BannerController {

    @Resource
    BannerService bannerService;

    /**
     * 初始化页面
     * @return
     */
    @RequestMapping("/init")
    public ModelAndView showIndex(){
        ModelAndView mav = new ModelAndView("/banner/bannerList");
        List<BannerEntity> bannerList = bannerService.getBanner(new BannerEntity());
        mav.addObject("menu","banner");
        mav.addObject("bannerList",bannerList);
        return mav;
    }

    /**
     * 修改状态（1：显示   2：隐藏）
     * @return
     */
    @RequestMapping("/editStatus")
    @ResponseBody
    public RespModel editStatus(BannerEntity bannerEntity){
        RespModel respModel = new RespModel();
        bannerEntity.setUpdateTime(new Date());
        bannerService.editBanner(bannerEntity);
        respModel.setCode(RespModel.RespCode.SUCCESS.getCode());
        return respModel;
    }

    /**
     * 修改banner资料
     * @return
     */
    @RequestMapping("/editBanner")
    public ModelAndView editBanner(BannerEntity bannerEntity){
        ModelAndView mav = new ModelAndView("/banner/bannerEdit");
        BannerEntity banner = bannerService.getBannerById(bannerEntity);
        mav.addObject("banner",banner);
        return mav;
    }
    /**
     * 修改banner资料
     * @return
     */
    @RequestMapping("/addBanner")
    public ModelAndView addBanner(){
        ModelAndView mav = new ModelAndView("/banner/bannerEdit");
        return mav;
    }
    /**
     * 修改banner资料
     * @return
     */
    @RequestMapping("/saveAndEdit")
    @ResponseBody
    public RespModel saveAndEdit(BannerEntity bannerEntity){
        RespModel respModel = new RespModel();
        try{
            Date now = new Date();
            if (bannerEntity != null && bannerEntity.getId() > 0){
                //修改
                bannerEntity.setUpdateTime(now);
                bannerService.editBanner(bannerEntity);
            }else{
                //新增
                bannerEntity.setCreateTime(now);
                bannerEntity.setUpdateTime(now);
                bannerService.addBanner(bannerEntity);
            }
            respModel.setCode(RespModel.RespCode.SUCCESS.getCode());
        }catch (Exception e){
            respModel.setCode(RespModel.RespCode.SYS_EXCEPTION.getCode());
        }
        return respModel;
    }

    /**
     * 修改banner资料
     * @return
     */
    @RequestMapping("/delBanner")
    @ResponseBody
    public RespModel delBanner(BannerEntity bannerEntity){
        RespModel respModel = new RespModel();
        bannerEntity.setDeleteFlag(1);
        bannerService.editBanner(bannerEntity);
        return respModel;
    }
}
